/*
 * FileName：AccountCtrl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.ctrl;

import java.util.List;

import com.smart.common.util.SessionUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.common.BaseCtrl;
import com.smart.common.util.AjaxResult;
import com.smart.wx.common.util.wx.WxUtil;
import com.smart.common.util.WxMemoryCacheClient;
import com.smart.wx.wxcms.entity.Account;
import com.smart.wx.wxcms.service.AccountService;

/**
 * @author hermit
 * @version 2.0
 * @date 2018-04-17 10:54:58
 */
@Controller
@RequestMapping("/account")
public class AccountCtrl extends BaseCtrl {

    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/getById")
    @ResponseBody
    public AjaxResult getById(long id) {
        accountService.getById(id);
        return AjaxResult.success();
    }

    @RequestMapping(value = "/listForPage")
    @ResponseBody
    public AjaxResult listForPage(Account account) {
        account.setUserId(SessionUtil.getUserId());
        List<Account> list = accountService.listForPage(account);
        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success();
        }
        // account存入缓存中
        if (null != account && null != account.getId()) {
            account = accountService.getById(account.getId());
            WxMemoryCacheClient.setAccount(account.getAccount());
        } else {
            account = accountService.getByAccount(WxMemoryCacheClient.getAccount());
        }
        if (null == account) {
            account = accountService.getSingleAccount(SessionUtil.getUserId());
        }
        if (null == account) {
            return AjaxResult.success();
        }
        return AjaxResult.success(WxUtil.getAccount(list, account.getName()));
    }

    /**
     * 删除公众号
     *
     * @param account
     * @return
     * @author hermit
     * @date 2018-02-23 11:02:18
     */
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(String account)
            throws Exception {
        this.accountService.delete(account);
        //更新缓存
        WxMemoryCacheClient.delMpAccount(account);
        return AjaxResult.success();
    }
}
